<template>
	<view>
		<view class="search-box">
			<uni-search-bar @confirm="search" @input="input" :radius="100" cancelButton=none focus=true>
			</uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResuts.length !== 0">
			<view class="sugg-item" v-for="(item,index) in searchResuts" :key="index" @click="gotoDetail(item)">
				<view class="sugg-name">{{item.goods_name}}</view>
				  <uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 历史搜索 -->
		<view class="history-box" v-else>
			<!-- 标题区 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 内容区 -->
			<view class="history-list">
				<uni-tag  v-for="(item,index) in history" :key="index" :text="item"
				custom-style="background-color: #efefef; border-color: #efefef; color: #000000; margin: 5px 5px 0 0;" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				time: null,
				// 输入的关键字
				kw: '',
				// 根据关键字返回的数据列表
				searchResuts: [],
				// 搜索历史记录
				historyList:[]
			};
		},
		onLoad() {
		this.historyList=JSON.parse(uni.getStorageSync('kw') || '[]')
			
		},
		methods: {
			input(e) {
				// 防抖处理，防止在短时间内多次触发
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchResut()
				}, 500)
			},
			async getSearchResut() {
				if (this.kw === '') {
					this.searchResuts = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResuts = res.message
				
				// 发送请求之后将关键词添加到历史记录的数组中
				this.saveSearchHistory()
			},
			gotoDetail(e){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+e.goods_id
				})
			},
			saveSearchHistory(){
				// 将关键词添加到历史记录的数组中
				// this.historyList.push(this.kw)
				
				// 创建一个Set对象
				const set = new Set(this.historyList)
				// 先移除kw在重新添加达到唯一值的效果
				set.delete(this.kw),
				set.add(this.kw),
				this.historyList=Array.from(set)
				
				// 对搜索历史数据进行持久化的存储
				// SON.stringify()是转换成数组
				// uni.setStorageSync（）可以将数据永久添加到本地存储
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			clean(){
				this.historyList=[],
				uni.setStorageSync('kw','[]')
			},
			gotoGoodsList(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			}
		},
		computed:{
			history(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;

		.uni-searchbar {
			background-color: #c00000;
		}
	}
	.sugg-list{
		padding: 0 5px;
		.sugg-item{
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.sugg-name{
				// 文字不允许换行（单行文本）
				white-space: nowrap;
				// 隐藏移除部分
				overflow: hidden;
				// 用···代替溢出部分
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
	
	.history-box{
		padding: 0 5px;
		.history-title{
			height: 40px;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 13px;
			// 添加下边框
			border-bottom: 1px solid #efefef;
			.history-list{
				display: flex;
				flex-wrap: wrap;
			}
		}
	}
</style>
